<?php
/**
* 预约管理
* @date: 2017年7月28日 下午11:03:08
* @author: hxzlh
*/
namespace app\admin\controller;

use think\Request;
use app\admin\model\Appointment as AppointmentMode;
use app\admin\model\Student as StudentMode;

class Appointment extends Admin
{

    /**
     * XXX
     * 预约管理列表
     */
    public function index()
    {
        if (Request::instance()->has('name','param')) {
            $name = Request::instance()->param('name');
            if(is_numeric($name)){
                $map['id|name']=   array('like','%'.$name.'%');
            }else{
                $map['name']    =   array('like', '%'.(string)$name.'%');
            }
        }
        if (Request::instance()->has('state','param')) {
            $map['state']    =  Request::instance()->param('state');
        }

        $list   = $this->lists('Appointment', $map, 'update_time desc');
        int_to_string($list, ['state'=>['未处理','已电话沟通','已预约体验','已体验','已购课','已放弃','已注册'],'gender'=>['未知','男','女']]);
        $this->assign('_list', $list);
        $this->assign('meta_title','预约列表');
        return $this->fetch();
    }

    /**
     * XXX
     * 编辑预约
     * @date: 2017年7月27日 下午11:59:13
     * @return: return
     */
    public function edit()
    {
        $model = new AppointmentMode();
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'Appointment');
            if(true !== $result){
                return $this->error($result);
            }
            $res = $model->update($data);
            if($res){
                $this->success('编辑成功！', url('index'));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('id');
            empty($id) && $this->error('参数不能为空！');

            $info = $model->find($id);
            $this->assign('_info',       $info);
            $this->assign('meta_title' ,'编辑预约');
            return $this->fetch();
        }
    }
    /**
     * XXX
     * 新增预约
     * @date: 2017年7月27日 下午11:07:29
     * @return mixed|string
     * @return: return
     */
    public function add()
    {
        if(Request::instance()->isPost()){
            //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'Appointment');
            if(true !== $result){
                return $this->error($result);
            }
            $res = AppointmentMode::create($data);
            if($data){
                $this->success('新增成功','index');
            } else {
                $this->error($result);
            }
        } else {
            $info = [];
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'新增预约');
            return $this->fetch('edit');
        }
    }
    /**
     * XXX
     * 更改预约状态
     * @date: 2017年7月28日 上午12:25:21
     * @return: return
     */
    public function changeState($state=null)
    {
        $ids = Request::instance()->param('id/a');
        $id = array_unique($ids);
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ($state){
            case 0:
                $data['state'] = 0;
                break;
            case 1:
                $data['state'] = 1;
                break;
            case 2:
                $data['state'] = 2;
                break;
            case 3:
                $data['state'] = 3;
                break;
            case 4:
                $data['state'] = 4;
                break;
            case 5:
                $data['state'] = 5;
                break;
            default:
                $this->error('参数非法');
        }
        $data['update_time'] = NOW_TIME;
        $this->editRow('Appointment', $data, $map);
    }
    
    public function register() 
    {
        $id = Request::instance()->param('id');
        empty($id) && $this->error('参数不能为空！');
        $info = AppointmentMode::get($id);
        $data = [
                'name'=>$info['name'],
                'gender'=>$info['gender'],
                'phone'=>$info['phone'],
                'level'=>$info['level'],
        ];
        
        $result = $this->validate($data,'Student');
        if(true !== $result){
            return $this->error($result);
        }
        $res = StudentMode::create($data);
        if($data){
            $info->state = 6;
            $info->save();
            $this->success('注册成功','index');
        } else {
            $this->error($result);
        }
        
    }

}
